﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Web;

namespace MediSIIM.BizComponent.HangZhouSYB
{
    public class YiBaoJKDLL
    {
        [DllImport("MCIF.dll", CharSet = CharSet.Ansi)]
        private static extern int MCIF(
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string TransType,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string ProcessingCode,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string HosCode,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string OPTRCode,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string BusiCycle,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string CenterCode,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string SenderSerialNo,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string InputStr,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string InputStrAcc,
                                    int ItemNumberIndicator,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string ReceiverSerialNo,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string OutputStr,
                                    [MarshalAs(UnmanagedType.I4)]ref int outBufLen,
                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string ResponseCode);

        public static int Call(ref string TransType, string ProcessingCode, string HosCode, string OPTRCode, string BusiCycle, string CenterCode, string SenderSerialNo, string InputStr, string InputStrAcc, int ItemNumberIndicator, ref string ReceiverSerialNo, ref string OutputStr, ref int outBufLen, ref string ResponseCode)
        {
            var retcode = -1;
            var filename = GetLogFileName();
            if (!Directory.Exists(Path.GetDirectoryName(filename)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filename));
            }
            using (StreamWriter sw = new StreamWriter(filename, true, Encoding.UTF8))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff $$IN  ") +
                  "[TransType=" + TransType +
                  "][ProcessingCode=" + ProcessingCode +
                  "][HosCode=" + HosCode +
                  "][OPTRCode=" + OPTRCode +
                  "][BusiCycle=" + BusiCycle +
                  "][CenterCode=" + CenterCode +
                  "][SenderSerialNo=" + SenderSerialNo +
                  "][InputStr=" + InputStr +
                  "][InputStrAcc=" + InputStrAcc +
                  "][ItemNumberIndicator=" + ItemNumberIndicator +
                  "][ReceiverSerialNo=" + ReceiverSerialNo +
                  "][OutputStr=" + "" +//OutputStr
                  "][outBufLen=" + outBufLen +
                  "][ResponseCode=" + ResponseCode + "]");
                retcode =
                     MCIF(ref TransType,
                     ref ProcessingCode,
                     ref HosCode,
                     ref OPTRCode,
                     ref BusiCycle,
                     ref CenterCode,
                     ref SenderSerialNo,
                     ref InputStr,
                     ref InputStrAcc,
                     ItemNumberIndicator,
                     ref ReceiverSerialNo,
                     ref OutputStr,
                     ref outBufLen,
                     ref ResponseCode);
                OutputStr = OutputStr.TrimEnd(' ').TrimEnd('\0');
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff $$OUT ") +
                             "[TransType=" + TransType +
                             "][ProcessingCode=" + ProcessingCode +
                             "][HosCode=" + HosCode +
                             "][OPTRCode=" + OPTRCode +
                             "][BusiCycle=" + BusiCycle +
                             "][CenterCode=" + CenterCode +
                             "][SenderSerialNo=" + SenderSerialNo +
                             "][InputStr=" + InputStr +
                             "][InputStrAcc=" + InputStrAcc +
                             "][ItemNumberIndicator=" + ItemNumberIndicator +
                             "][ReceiverSerialNo=" + ReceiverSerialNo +
                             "][OutputStr=" + OutputStr.Trim() +//OutputStr
                             "][outBufLen=" + outBufLen +
                             "][ResponseCode=" + ResponseCode + "]");
            }
            return retcode;
        }

        #region TEST

        //    public static CallResult YBCall(EnumTradeTypeNo tradeTypeNo)
        //    {
        //        string ProcessingCode = "124777";
        //        string HosCode = "87e3";
        //        string OPTRCode = "324235";
        //        string BusiCycle = "232323";
        //        string CenterCode = "232323";
        //        string SenderSerialNo = "232323";
        //        string InputStr = "232323";
        //        string InputStrAcc = "232323";
        //        int ItemNumberIndicator = 222;
        //        string ReceiverSerialNo = "23232";
        //        string OutputStr = "232323";
        //        int outBufLen = 0;
        //        string ResponseCode = "242";
        //        return new CallResult("-1~~",
        //                Call(tradeTypeNo, ProcessingCode, HosCode, OPTRCode, BusiCycle, CenterCode, SenderSerialNo, InputStr, InputStrAcc, ItemNumberIndicator, ref  ReceiverSerialNo, ref  OutputStr, ref  outBufLen, ref  ResponseCode)
        //        );
        //    }
        //}

        #endregion

        public static string GetLogFileName()
        {
                return string.Format(@"{0}\HangZhouSYB\logs\SIIM{1:yyyyMMdd}-{2}.log", Application.StartupPath, DateTime.Now, Process.GetCurrentProcess().Id);
        }
    }
}
